﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Comm;
using VK.Casemeet.Merchants;

namespace VK.Casemeet.Web.Admin.Merchants
{
    public partial class ShopAdd : System.Web.UI.Page
    {
        public string map = CS.ShopMap;
        public string imgUrl = "Null.jpg", logoUrl = "Null.jpg";

        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/Admin/Home.aspx"},
                new MapPath{ Text="店铺",Url="/Admin/Merchants/ShopList.aspx"},
                new MapPath{ Text="新建店铺",Url="#"}
            };

            if (!IsPostBack)
            {
                ShopHelper.BindTypeDDL(ddlType, OptionType.Default);
                ShopHelper.BindStateDDL(ddlState, OptionType.Select);
                ShopHelper.BindLevelRAD(radLevel);
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            ShopEntity shop = LoadEntity();

            if (ShopBLL.InsertShop(shop) > 0)
            {
                Message.Msg(this.Page, CS.MsgAddSuccess, MessageType.Success);
                Util.ClearControl((this.Master as Site).contentBody.Controls);
                txtHits.Text = "0";
            }
            else
            {
                Message.Msg(this.Page, CS.MsgAddFailure, MessageType.Failure);
            }
        }

        protected ShopEntity LoadEntity()
        {
            ShopEntity shop = new ShopEntity();
            shop.Type = int.Parse(ddlType.SelectedValue);
            shop.Name = txtName.Text.Trim();
            shop.Level = int.Parse(radLevel.SelectedValue);
            shop.Telephone = txtTelephone.Text.Trim();
            shop.Capital = string.IsNullOrEmpty(txtCapital.Text.Trim()) ? 0 : int.Parse(txtCapital.Text.Trim());
            shop.ProvinceID = Util.GetFormInt("ddlArea1");
            shop.CityID = Util.GetFormInt("ddlArea2");
            shop.AreaID = Util.GetFormInt("ddlArea3");
            shop.Address = txtAddress.Text.Trim();
            shop.Map = Util.GetFormString("txtMap");
            shop.Telephone = txtTelephone.Text.Trim();
            shop.Hits = int.Parse(txtHits.Text.Trim());
            shop.IsCertification = chkIsCertification.Checked;
            shop.State = int.Parse(ddlState.SelectedValue);
            shop.Notice = txtNotice.Text.Trim();
            shop.LicenceUrl = Util.GetFormString("hidImgUrl");
            shop.Contact = txtContact.Text.Trim();
            shop.Email = txtEmail.Text.Trim();
            shop.Company = txtCompany.Text.Trim();
            shop.Account = txtAccount.Text.Trim();
            shop.Password = DES.Encrypt(Util.GetRandomNumber(6), CS.ShopAdminPassKey);
            shop.LogoUrl = Util.GetFormString("hidLogoUrl");
            shop.QQ = txtQQ.Text.Trim();
            shop.SiteUrl = txtSiteUrl.Text.Trim();

            return shop;
        }
    }
}